#!/usr/bin/python
# -*- coding: utf-8 -*-

import psycopg2
import sys
import cgi
import json
import db_conf
import time
con = None

def archive_compound_sql(id):
    return "UPDATE compound SET archived = 1 WHERE compound_id = " + id

def archive_spectrum_sql( id ):
    return "UPDATE spectrum SET archived = 1 WHERE spectrum_id = " + id 

def archive_spectrum_annotation_sql():
    return """UPDATE spectrum_annotation SET archived = 1 WHERE compound_id = %s AND mz = %s"""

def archive_experiment_sql( id ):
    return "UPDATE experiment SET archived = 1 WHERE experiment_id = " + id

def delete_precursor_sql():
    return """DELETE FROM compound_derivatization where product_compound = %s AND precursor_compound = %s AND derivatization_reagent_id = %s limit 1"""

def delete_chromatogram_sample_sql():
    return """DELETE FROM chromatogram_sample_group where chromatogram_id = %s AND sample_group_id = %s AND experiment_id = %s"""

try:
    qform = cgi.FieldStorage()
    datatype = qform.getvalue('datatype')	   
    compound_id = qform.getvalue('compound_id')   
    experiment_id = qform.getvalue('experiment_id')
    chromatogram_id = qform.getvalue('chromatogram_id')	
    sample_group_id = qform.getvalue('sample_group_id')
    peak_mz = qform.getvalue('spa_peak_mz')
    spectrum_id = qform.getvalue('spectrum_id')
    precursor_compound = qform.getvalue('precursor_compound')
    reagent = qform.getvalue('reagent')
    formula = qform.getvalue('original_formula')
    config = db_conf.getConfig('metabvis.conf')
    con = psycopg2.connect(host=db_conf.getDBHost(config), database=db_conf.getDBName(config), port=db_conf.getDBPort(config), user=db_conf.getUser(config), password=db_conf.getPassword(config)) 
    cur = con.cursor()
    if (datatype == "spectrum_annotation"):
    	cur.execute(archive_spectrum_annotation_sql(), (compound_id, peak_mz))
    if (datatype == "spectrum"):
        cur.execute(archive_spectrum_sql(spectrum_id))
    if (datatype == "experiment"):
        cur.execute(archive_experiment_sql(experiment_id))
    if (datatype == "compound"):
        cur.execute(archive_compound_sql(compound_id))
    if (datatype == "compound_precursor"):
        cur.execute(delete_precursor_sql(), (compound_id, precursor_compound, reagent))
        if (formula != None):
            if (formula == "fnone"):
                formula = ""
            cur.execute("""UPDATE compound SET sum_formula = %s WHERE compound_id = %s""", (formula, compound_id))
        else:
            cur.execute("UPDATE compound SET sum_formula = '' WHERE compound_id = " + compound_id)
    if (datatype == "chromatogram_sample_group"):
        cur.execute(delete_chromatogram_sample_sql(), (chromatogram_id, sample_group_id, experiment_id))
    cur.execute("commit")
    print "Content-type: text/html;charset=utf-8\r\n"
    pa = {}
    pa['_status'] = "200"
    print json.dumps(pa)
except psycopg2.DatabaseError, e:
    print 'Update Error %s' % e    
    sys.exit(1)    
finally:
    if con:
        con.close()
